#if 0

#include <sstream>
#include <iomanip>
#include <iostream>
using namespace std;
#include "DataManager.h"


#define UserFile "../Info/User.txt"
#define PartFile "../Info/Part.txt"
#define StoreInFile "../Info/StoreIn.txt"
#define StoreOutFile "../Info/StoreOut.txt"
#define CurrentStoreAnomalyFile "../Info/CurrentStoreAnomaly.txt"
#define AllStoreAnomalyFile "../Info/AllStoreAnomaly.txt"
#define LogFile "../Info/Log.txt"



// 将myTime_t字符串转换为std::tm结构体
std::tm stringToTm(const myTime_t& str)
{
    std::tm tm = {};
    std::istringstream ss(str);
    ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S");
    return tm;
}

//sort比较规则
template <class T>
bool compare(T u1, T u2) { return u1->getId() < u2->getId(); }

/*===========================<管理基类>===========================*/
MYPTR_T& BaseManager::getUser(void) { return this->user; }

void BaseManager::setUser(const MYPTR_T& user) { this->user = user; }

int BaseManager::load_UserFromFile()
{
    Admin tmpUser(10000, "admin", "000000", (PB_t)UPBits::USER_ADMIN_ALL, "系统首次初始化的管理员账号");
    tmpUser.setRole(Role::ADMIN);
    string line;

    ifstream ifs(UserFile, ios::in);

    if (!ifs.is_open())
    {
        ofstream ofs(UserFile, ios::out);
        if (!ofs.is_open())
        {
            perror("ofs open failed");
            return -1;
        }

        this->loginLib.push_back(std::make_shared<Admin>(tmpUser));
        ofs << tmpUser.serialize();
        ofs.close();
        return 0;
    }

    else
    {
        loginLib.clear();
        while (getline(ifs, line))
        {
            tmpUser.deSerialize(line);
            switch (tmpUser.getRole())
            {
                case Role::ADMIN:
                    this->loginLib.push_back(std::make_shared<Admin>(tmpUser));break;
                case Role::USER_INFO:
                    this->loginLib.push_back(std::make_shared<User_info>(tmpUser));break;
                case Role::USER_STORE_CHECK:
                    this->loginLib.push_back(std::make_shared<User_storeCheck>(tmpUser));break;
                case Role::USER_STORE_IO:
                    this->loginLib.push_back(std::make_shared<User_storeIO>(tmpUser));break;
                default:
                    break;
            }  
        }
        ifs.close();
        return 0; 
    }
}

// 登录验证
MYPTR_T BaseManager::login_verification(const std::string& name, const std::string& password)
{
    auto it = find_if(this->loginLib.begin(), this->loginLib.end(), [&name, &password, this](MYPTR_T user) {return ((*user).getName() == name && (*user).getPassword() == password); });
    if (it == this->loginLib.end())
        return nullptr;

    return (*it);
}


/*===========================<用户管理类(管理员界面使用)>===========================*/

//加载数据，首次打开系统创建一个管理员账号
int UserManager::load_UserFromFile()
{
    Admin tmpUser(10000, "admin", "000000", (PB_t)UPBits::USER_ADMIN_ALL, "系统首次初始化的管理员账号");
    tmpUser.setRole(Role::ADMIN);
    string line;

    ifstream ifs(UserFile, ios::in);

    if (!ifs.is_open())
    {
        ofstream ofs(UserFile, ios::out);
        if (!ofs.is_open())
        {
            perror("ofs open failed");
            return -1;
        }

        this->userLib.push_back(std::make_shared<User>(tmpUser));
        ofs << tmpUser.serialize();
        ofs.close();
        return 0;
    }

    else
    {
        userLib.clear();
        while (getline(ifs, line))
        {
            tmpUser.deSerialize(line);
            switch (tmpUser.getRole())
            {
                case Role::ADMIN:
                    this->userLib.push_back(std::make_shared<Admin>(tmpUser));break;
                case Role::USER_INFO:
                    this->userLib.push_back(std::make_shared<User_info>(tmpUser));break;
                case Role::USER_STORE_CHECK:
                    this->userLib.push_back(std::make_shared<User_storeCheck>(tmpUser));break;
                case Role::USER_STORE_IO:
                    this->userLib.push_back(std::make_shared<User_storeIO>(tmpUser));break;
                default:
                    break;
            }  
        }
        ifs.close();
        return 0; 
    }
}

int UserManager::save_UserToFile()
{
    ofstream ofs(UserFile, ios::out);
    if (!ofs.is_open())
    {
        perror("ofs open failed");
        return -1;
    }

    for (vector<MYPTR_T>::iterator it = this->userLib.begin(); it != this->userLib.end(); ++it)
    {
        ofs<<(*(*it)).serialize();
    }
    return 0;
}


int UserManager::info_sort()
{
    sort(this->userLib.begin(), this->userLib.end(), compare<shared_ptr<User>>);
    return 0;
}

int UserManager::show_UserInfo() 
{
    for (auto it = this->userLib.begin(); it != this->userLib.end(); ++it)
        cout << (*(*it)).serialize();
    return 0;
}

int UserManager::add_info(User& user, Role role)
{
    Admin tmpAdmin(0, "", "", (PB_t)1, "");
    if (tmpAdmin.add_user(this->userLib, user, role) != 0)
        return -1;
    return 0;
}

int UserManager::delete_info(int id)
{
    Admin tmpAdmin(0, "", "", (PB_t)0, "");
    if (tmpAdmin.delete_user(this->userLib, id) != 0)
        return -1;
    return 0;
}

int UserManager::modify_info(int id, const User& user)
{
    Admin tmpAdmin(0, "", "", (PB_t)0, "");
    if (tmpAdmin.modify_user(this->userLib, id, user) != 0)
        return -1;
    return 0;
}

MYPTR_T UserManager::find_info(int id)
{
    Admin tmpAdmin(0, "", "", (PB_t)0, "");
    MYPTR_T tmp;
    if ((tmp = tmpAdmin.find_user(this->userLib, id)) == nullptr)
        return nullptr;
    
    return tmp;
}


/*===========================<配件信息管理类实现，User_info界面使用>===========================*/
std::vector<std::shared_ptr<Part>>& PartManager::getPartLib(){ return this->partLib; }
void PartManager::setPartLib(std::vector<std::shared_ptr<Part>> partLib) { this->partLib = partLib; }


int PartManager::load_PartFromFile()
{
    Part tmpPart;
    string line;

    ifstream ifs(PartFile, ios::in);
    if (!ifs.is_open())
    {
        perror("Load PartFile failed but create PartFile");
        ofstream ofs(PartFile, ios::out);
        ofs.close();
        return -1;
    }

    this->partLib.clear();

    while (getline(ifs, line))
    {
        tmpPart.deSerialize(line);
        this->partLib.push_back(std::make_shared<Part>(tmpPart));
    }
    ifs.close();

    return 0;
}

int PartManager::save_PartToFile()
{
    ofstream ofs(PartFile, ios::out);
    if (!ofs.is_open())
    {
        perror("Save open partfile failed");
        return -1;
    }

    for (vector<shared_ptr<Part>>::iterator it = this->partLib.begin(); it != this->partLib.end(); ++it)
    {
        ofs << (*it)->serialize();
    }
    ofs.close();

    return 0;
}

int PartManager::info_sort()
{
    sort(partLib.begin(), partLib.end(), compare<shared_ptr<Part>>);
    return 0;
}

int PartManager::show_PartInfo()
{
    for (auto it = partLib.begin(); it != partLib.end(); ++it)
        cout << (*it)->serialize();
    if (this->partLib.empty() == 1)
        return -1;
    return 0;
}

int PartManager::show_not_in_store_info()
{
    int count = 0;
    for (auto it = partLib.begin(); it != partLib.end(); ++it)
    {
        if ((*it)->getLocation() == "未分配位置")
        {
            cout << (*it)->serialize();
            count++;
        }
    }
    if (this->partLib.empty() == 1)
        return -1;
    return count;
}

int PartManager::assigning_partLocation(int partId, std::string location)
{
    std::shared_ptr<Part> tmpPart;
    tmpPart = PartManager::find_info(partId,"未分配位置");
    if (tmpPart == nullptr)
        return -1;
    tmpPart->setLocation(location);
    return 0;
}

int PartManager::add_info(Part& part)
{
    InfoManagerTemplate<Part> tmpInfoManagerTemplate(Part::getId);

    if (tmpInfoManagerTemplate.add_info(this->partLib, part) != 0)
        return -1;
     
    return 0;
}

int PartManager::delete_info(int id)
{
    InfoManagerTemplate<Part> tmpInfoManagerTemplate(Part::getId);
    if (tmpInfoManagerTemplate.delete_info(this->partLib, id)!=0)
        return -1;
    return 0;
}

int PartManager::modify_info(int id, const Part& part)
{
    InfoManagerTemplate<Part> tmpInfoManagerTemplate(Part::getId);
    if (tmpInfoManagerTemplate.modify_info(this->partLib, id, part) != 0)
        return -1;
    return 0;
}
int PartManager::modify_info(int id, std::string location, const Part& part)
{
    InfoManagerTemplate<Part> tmpInfoManagerTemplate(Part::getId, Part::getLocation);
    if (tmpInfoManagerTemplate.modify_info(this->partLib, id, location, part) != 0)
        return -1;
    return 0;
}
shared_ptr<Part> PartManager::find_info(int id)
{
    InfoManagerTemplate<Part> tmpInfoManagerTemplate(Part::getId);

    if (tmpInfoManagerTemplate.find_info(this->partLib, id) != nullptr)
        return tmpInfoManagerTemplate.find_info(this->partLib, id);
    return nullptr;
}


shared_ptr<Part> PartManager::find_info(int id, string location)
{
    InfoManagerTemplate<Part> tmpInfoManagerTemplate(Part::getId, Part::getLocation);
    if (tmpInfoManagerTemplate.find_info(this->partLib, id, location) != nullptr)
        return tmpInfoManagerTemplate.find_info(this->partLib, id, location);
    return nullptr;
}

int PartManager::find_info_all(int id)
{
    int findCount = 0;
    if (this->partLib.empty() == 1)
        return -1;
    for (auto it = partLib.begin(); it != partLib.end(); ++it)
    {
        if ((*it)->getId() == id)
        {
            cout << (*it)->serialize();
            findCount++;
        }
    }
    return 0;
}

/*===========================<仓库入库信息管理类实现，入库信息界面使用>===========================*/

int StoreInManager::load_StoreFromFile()
{
    string line;
    StoreInRecord tmpInRecord;
    this->storeInInfoLib.clear();

    ifstream ifs(StoreInFile, ios::in);//加载入库记录下信息
    if (!ifs.is_open())
    {
        ofstream ofs(StoreInFile, ios::out);
        ofs.close();
        return -1;
    }

    while (getline(ifs, line))
    {
        tmpInRecord.deSerialize(line);
        this->storeInInfoLib.push_back(make_shared<StoreInRecord>(tmpInRecord));
    }
    ifs.close();

    return 0;
}

int StoreInManager::save_StoreToFile()
{
    ofstream ofs(StoreInFile, ios::out);
    if (!ofs.is_open())
    {
        perror("Save open StoreInFile failed");
        return -1;
    }
    for (auto it = this->storeInInfoLib.begin(); it != this->storeInInfoLib.end(); ++it)
        ofs << (*it)->serialize();

    ofs.close();

    return 0;
}

int StoreInManager::add_info(StoreInRecord& storeInfo)
{
    InfoManagerTemplate<StoreInRecord> infoManager(StoreInRecord::getrecordId);
    if (infoManager.add_info(this->storeInInfoLib, storeInfo) != 0)
        return -1;
    
    return 0;
}
int StoreInManager::delete_info(int id)
{
    InfoManagerTemplate<StoreInRecord> infoManager(StoreInRecord::getrecordId);
    if (infoManager.delete_info(this->storeInInfoLib, id) != 0)
        return -1;
    return 0;
}
int StoreInManager::modify_info(int id, const StoreInRecord& storeRecord)
{
    InfoManagerTemplate<StoreInRecord> infoManager(StoreInRecord::getrecordId);
    if (infoManager.modify_info(this->storeInInfoLib, id, storeRecord) != 0)
        return -1;

    return 0;
}
std::shared_ptr<StoreInRecord> StoreInManager::find_info(int id)
{
    InfoManagerTemplate<StoreInRecord> infoManager(StoreInRecord::getrecordId);
    std::shared_ptr<StoreInRecord> tmp;

    tmp = infoManager.find_info(this->storeInInfoLib, id);

    return tmp;
}

int StoreInManager::get_record_endId(void)
{
    if (this->storeInInfoLib.empty())
        return 0;
    return (*(this->storeInInfoLib.end() - 1))->getrecordId();
}

int StoreInManager::show_StoreInfo()
{
    for (auto it = this->storeInInfoLib.begin(); it != this->storeInInfoLib.end(); ++it)
        cout << (*it)->serialize();
    return 0;
}


/*===========================<仓库出库信息管理类实现，出库信息界面使用>===========================*/

int StoreOutManager::load_StoreFromFile()
{
    string line;
    StoreOutRecord tmpOutRecord;

    this->storeOutInfoLib.clear();

    ifstream ifs(StoreOutFile, ios::in);
    if (!ifs.is_open())
    {
        ofstream ofs(StoreOutFile, ios::out);
        ofs.close();
        return -1;
    }

    while (getline(ifs, line))
    {
        tmpOutRecord.deSerialize(line);
        this->storeOutInfoLib.push_back(make_shared<StoreOutRecord>(tmpOutRecord));
    }
    ifs.close();

    return 0;
}

int StoreOutManager::save_StoreToFile()
{
    ofstream ofs(StoreOutFile, ios::out);
    if (!ofs.is_open())
    {
        perror("Save open StoreOutFile failed");
        return -1;
    }
    for (auto it = this->storeOutInfoLib.begin(); it != this->storeOutInfoLib.end(); ++it)
        ofs << (*it)->serialize();

    ofs.close();
    return 0;
}


int StoreOutManager::add_info(StoreOutRecord& storeInfo)
{
    InfoManagerTemplate<StoreOutRecord> infoManager(StoreOutRecord::getrecordId);
    infoManager.add_info(this->storeOutInfoLib, storeInfo);
    return 0;
}

int StoreOutManager::delete_info(int id)
{
    InfoManagerTemplate<StoreOutRecord> infoManager(StoreOutRecord::getrecordId);
    if (infoManager.delete_info(this->storeOutInfoLib, id) != 0)
        return -1;
    return 0;

}

int StoreOutManager::modify_info(int id, const StoreOutRecord& storeRecord)
{
    InfoManagerTemplate<StoreOutRecord> infoManager(StoreOutRecord::getrecordId);
    if (infoManager.modify_info(this->storeOutInfoLib, id, storeRecord) != 0)
        return -1;

    return 0;
}

std::shared_ptr<StoreOutRecord> StoreOutManager::find_info(int id)
{
    InfoManagerTemplate<StoreOutRecord> infoManager(StoreOutRecord::getrecordId);
    std::shared_ptr<StoreOutRecord> tmpInfo = infoManager.find_info(this->storeOutInfoLib, id);
    return tmpInfo;

}

int StoreOutManager::get_record_endId()
{
    if (storeOutInfoLib.empty())
        return 0;
    return (*(storeOutInfoLib.end() - 1))->getrecordId();
}

int StoreOutManager::show_StoreInfo()
{
    for (auto it = this->storeOutInfoLib.begin(); it != this->storeOutInfoLib.end(); ++it)
        cout << (*it)->serialize();
    return 0;
}


/*===========================<仓库出库入库信息管理基类实现>===========================*/

int StoreBaseManager::store_out(std::vector<shared_ptr<Part>>& partLib, int id, std::string location, int outQuantity)
{
    int realQuantity = outQuantity;

    /*=========记录出库逻辑=========*/
    StoreOutManager tmpStoreOutManager;
    StoreOutRecord tmpStoreOutRecord;
    std::time_t currentTime = std::time(nullptr);
    struct tm* localTime = std::localtime(&currentTime);

    std::ostringstream oss;
    // 自定义格式，例如 "YYYY-MM-DD HH:MM:SS"
    oss << std::put_time(localTime, "%Y-%m-%d %H:%M:%S");
    std::string timeString = oss.str();

    tmpStoreOutManager.load_StoreFromFile();
    
    /*=========记录出库逻辑=========*/

    /*=========出库逻辑=========*/
    InfoManagerTemplate<Part> infoManager(Part::getId, Part::getLocation);
    shared_ptr<Part> tmpPart = infoManager.find_info(partLib, id, location);


    if (tmpPart == nullptr)
    {
        tmpStoreOutRecord = StoreOutRecord(tmpStoreOutManager.get_record_endId()+1,id,timeString,"出库失败，库中没有找到此配件",0);
        
        realQuantity = -1;
    }
        

    else if (tmpPart->getStockQuantity() < outQuantity)
    {
        tmpStoreOutRecord = StoreOutRecord(tmpStoreOutManager.get_record_endId()+1,id,timeString,"出库失败，库中没有足够的此配件",0);
        realQuantity = 0;
    }
        
    else
    {
        tmpStoreOutRecord = StoreOutRecord(tmpStoreOutManager.get_record_endId()+1,id,timeString,"出库成功",outQuantity);
        tmpPart->setStockQuantity(tmpPart->getStockQuantity() - outQuantity);
        realQuantity = outQuantity;
    }
    
    /*=========出库逻辑=========*/

    
    tmpStoreOutManager.add_info(tmpStoreOutRecord);
    tmpStoreOutManager.save_StoreToFile();
    return realQuantity;//返回实际出库数量
}

//入库大于库存上限，返回实际入库数量，自动添加待分配配件信息
//1.为库存达到上限的配件分配新的ID(需分配唯一的ID)
//2.为库存达到上限的配件分配一样的ID，但是库存位置不同(需新增按照库存位置搜索)(√)
int StoreBaseManager::store_in(std::vector<shared_ptr<Part>>& partLib, int id, std::string location, int inQuantity)
{

    /*=========记录入库逻辑=========*/
    StoreInManager tmpStoreInManager;
    StoreInRecord tmpStoreInRecord;

    std::time_t currentTime = std::time(nullptr);
    struct tm* localTime = std::localtime(&currentTime);

    std::ostringstream oss;
    // 自定义格式，例如 "YYYY-MM-DD HH:MM:SS"
    oss << std::put_time(localTime, "%Y-%m-%d %H:%M:%S");
    std::string timeString = oss.str();

    tmpStoreInManager.load_StoreFromFile();

    /*=========记录入库逻辑=========*/

    /*=========入库逻辑=========*/
    InfoManagerTemplate<Part> infoManager(Part::getId, Part::getLocation);
    shared_ptr<Part> tmpPart = infoManager.find_info(partLib, id, location);
    shared_ptr<Part> tmpNewPart = make_shared<Part>(*tmpPart);

    int tmpRealQuantity = inQuantity;

    if (tmpPart == nullptr)
        return -1;

    if (tmpPart->getStockQuantity() + inQuantity > tmpPart->getStockUpperLimit())//如果入库数量超过上限
    {
        tmpRealQuantity = tmpPart->getStockUpperLimit() - tmpPart->getStockQuantity();//真实入库数量
        tmpPart->setStockQuantity(tmpPart->getStockUpperLimit());//库满

        tmpNewPart->setLocation("未分配位置");
        tmpNewPart->setStockQuantity(inQuantity + tmpNewPart->getStockQuantity() - tmpNewPart->getStockUpperLimit());
        infoManager.add_info(partLib, *tmpNewPart);

        tmpStoreInRecord = StoreInRecord(tmpStoreInManager.get_record_endId()+1,id,timeString,"入库成功一部分，剩余配件待配件录入人员分配位置",tmpRealQuantity);
    }
    else
    {
        tmpPart->setStockQuantity(tmpPart->getStockQuantity() + inQuantity);
        tmpStoreInRecord = StoreInRecord(tmpStoreInManager.get_record_endId()+1,id,timeString,"全部入库成功",inQuantity);
    }
    /*=========入库逻辑=========*/

    tmpStoreInManager.add_info(tmpStoreInRecord);
    tmpStoreInManager.save_StoreToFile();
    return tmpRealQuantity;

}


/*===========================<仓库盘点管理类实现>===========================*/

int StoreAnomalyManager::load_CurrentStoreAnomalyFromFile()
{
    StoreAnomaly tmpStoreAnomaly;
    string line;

    this->currentAnomalyLib.clear();
    
    ifstream ifs(CurrentStoreAnomalyFile, ios::in);
    if (!ifs.is_open())
    {
        ofstream ofs(CurrentStoreAnomalyFile, ios::out);
        ofs.close();
        return -1;
    }

    while (getline(ifs, line))
    {
        tmpStoreAnomaly.deSerialize(line);
        this->currentAnomalyLib.push_back(std::make_shared<StoreAnomaly>(tmpStoreAnomaly));
    }
    ifs.close();

    return 0;
}
int StoreAnomalyManager::load_AllStoreAnomalyFromFile()
{
    StoreAnomaly tmpStoreAnomaly;
    string line;
    
    this->allAnomalyLib.clear();
    
    ifstream ifs(AllStoreAnomalyFile, ios::in);
    if (!ifs.is_open())
    {
        ofstream ofs(AllStoreAnomalyFile, ios::out);
        ofs.close();
        return -1;
    }

    while (getline(ifs, line))
    {
        tmpStoreAnomaly.deSerialize(line);
        this->allAnomalyLib.push_back(std::make_shared<StoreAnomaly>(tmpStoreAnomaly));
    }
    ifs.close();

    return 0;
}
int StoreAnomalyManager::save_CurrentStoreAnomalyToFile()
{
    ofstream ofs(CurrentStoreAnomalyFile, ios::out);
    if (!ofs.is_open())
    {
        perror("Save open CurrentStoreAnomalyFile failed");
        return -1;
    }
    for (auto it = this->currentAnomalyLib.begin(); it != this->currentAnomalyLib.end(); ++it)
        ofs << (*it)->serialize();

    ofs.close();
    return 0;
}
int StoreAnomalyManager::save_AllStoreAnomalyToFile()
{
    ofstream ofs(AllStoreAnomalyFile, ios::out);
    if (!ofs.is_open())
    {
        perror("Save open ALLStoreAnomalyFile failed");
        return -1;
    }
    for (auto it = this->allAnomalyLib.begin(); it != this->allAnomalyLib.end(); ++it)
        ofs << (*it)->serialize();

    ofs.close();
    return 0;
}

int StoreAnomalyManager::show_CurrentStoreAnomaly()
{
    for (auto it = this->currentAnomalyLib.begin(); it != this->currentAnomalyLib.end(); ++it)
        cout << (*it)->serialize();
    return 0;
}
int StoreAnomalyManager::show_AllStoreAnomaly()
{
    for (auto it = this->allAnomalyLib.begin(); it != this->allAnomalyLib.end(); ++it)
        cout << (*it)->serialize();
    return 0;
}

int StoreAnomalyManager::add_info(StoreAnomaly& anomaly)
{
    InfoManagerTemplate<StoreAnomaly> tmpStoreAnomalytManagerTemplate(StoreAnomaly::getAnomalyId);

    tmpStoreAnomalytManagerTemplate.add_info(currentAnomalyLib, anomaly);
    anomaly.setAnomalyId(StoreAnomalyManager::get_all_anomaly_endId()+1);
    tmpStoreAnomalytManagerTemplate.add_info(allAnomalyLib, anomaly);

    return 0;
}

int StoreAnomalyManager::delete_info(int id)
{
    InfoManagerTemplate<StoreAnomaly> tmpStoreAnomalytManagerTemplate(StoreAnomaly::getAnomalyId);
    if (tmpStoreAnomalytManagerTemplate.delete_info(this->allAnomalyLib,id) != 0)
        return -1;
    return 0;
}

int StoreAnomalyManager::modify_info(int id, const StoreAnomaly& storeAnomaly)
{
    InfoManagerTemplate<StoreAnomaly> tmpStoreAnomalytManagerTemplate(StoreAnomaly::getAnomalyId);
    if (tmpStoreAnomalytManagerTemplate.modify_info(this->allAnomalyLib, id, storeAnomaly) != 0)
        return -1;
    return 0;
}

std::shared_ptr<StoreAnomaly> StoreAnomalyManager::find_info(int id)
{
    InfoManagerTemplate<StoreAnomaly> tmpStoreAnomalytManagerTemplate(StoreAnomaly::getAnomalyId);
    shared_ptr<StoreAnomaly> tmpInfo = tmpStoreAnomalytManagerTemplate.find_info(this->allAnomalyLib, id);
    return tmpInfo;
}

std::shared_ptr<StoreAnomaly> StoreAnomalyManager::find_info_current(int id)
{
    InfoManagerTemplate<StoreAnomaly> tmpStoreAnomalytManagerTemplate(StoreAnomaly::getAnomalyId);
    shared_ptr<StoreAnomaly> tmpInfo = tmpStoreAnomalytManagerTemplate.find_info(this->currentAnomalyLib, id);
    return tmpInfo;
}


int StoreAnomalyManager::get_current_anomaly_endId(void)
{
    if (currentAnomalyLib.empty())
        return 0;
    return (*(currentAnomalyLib.end() - 1))->getAnomalyId();
}

int StoreAnomalyManager::get_all_anomaly_endId(void)
{
    if (allAnomalyLib.empty())
        return 0;
    return (*(allAnomalyLib.end() - 1))->getAnomalyId();
}


int StoreAnomalyManager::compare_to_lib(std::vector<std::shared_ptr<Part>>& partLib, std::vector <std::shared_ptr<StoreCompare>> compareLib, int checkCount)
{
    StoreAnomaly tmpStoreAnomaly;//临时异常记录对象
    PartManager tmpPartManager;//临时配件管理对象
    InfoManagerTemplate<Part> tmpPartManagerTemplate(Part::getId, Part::getLocation);

    shared_ptr<Part> tmpPart;

    string difference;

    this->currentAnomalyLib.clear();
    this->save_CurrentStoreAnomalyToFile();

    for (int i = 0;i < checkCount;i++)
    {
        tmpPart = tmpPartManagerTemplate.find_info(partLib, compareLib[i]->partId, compareLib[i]->location);//在配件库中根据配件ID和位置查找
        
        if (tmpPart == nullptr)//如果没找到
            tmpStoreAnomaly = StoreAnomaly(StoreAnomalyManager::get_current_anomaly_endId() + 1, compareLib[i]->partId, compareLib[i]->location, compareLib[i]->realQuantity, 0, "找不到该配件", "未找到");
        
        else if (tmpPart->getStockQuantity() != compareLib[i]->realQuantity)
        {
            difference = "相差" + to_string((tmpPart->getStockQuantity() - compareLib[i]->realQuantity) > 0 ? (tmpPart->getStockQuantity() - compareLib[i]->realQuantity) : -(tmpPart->getStockQuantity() - compareLib[i]->realQuantity));
            tmpStoreAnomaly = StoreAnomaly(StoreAnomalyManager::get_current_anomaly_endId() + 1, compareLib[i]->partId, compareLib[i]->location, compareLib[i]->realQuantity, tmpPart->getStockQuantity(), difference, "未确认");
            
        }
        StoreAnomalyManager::add_info(tmpStoreAnomaly);
    }
    return 0;
}



int StoreAnomalyManager::confirm_differences(std::vector<std::shared_ptr<Part>>& partLib)
{
    std::shared_ptr<Part> tmpPart;
    InfoManagerTemplate<Part> tmpInfoManager(Part::getId, Part::getLocation);

    int tmpId;

    for (auto it = currentAnomalyLib.begin(); it != currentAnomalyLib.end(); ++it)
    {
        if ((*it)->getConfirmStatus() == "未确认")
        {
            tmpPart = tmpInfoManager.find_info(partLib, (*it)->getPartId(), (*it)->getLocation());
            if (tmpPart != nullptr)
            {
                tmpPart->setStockQuantity((*it)->getActualQuantity());
                (*it)->setConfirmStatus("已确认");
            }
            else
            {
                (*it)->setConfirmStatus("未找到");
            }
            // tmpInfoManager.modify_info((*it)->getAnomalyId(), (*it));//由于直接操纵的库先不加
        }
    }

    /*同步到历史差异中*/
    for (auto it = this->allAnomalyLib.end() - this->currentAnomalyLib.size(), it2 = this->currentAnomalyLib.begin(); it != this->allAnomalyLib.end() && it2 != this->currentAnomalyLib.end(); ++it, ++it2)
    {
        tmpId = (*it)->getAnomalyId();
        this->modify_info(tmpId, **it2);
        (*it)->setAnomalyId(tmpId);
    }
    /*同步到历史差异中*/

    return 0;
}
int StoreAnomalyManager::confirm_differences(std::vector<std::shared_ptr<Part>>& partLib, int confirmCount, std::vector<int> confirmIdLib)
{
    InfoManagerTemplate<Part> tmpInfoManager(Part::getId, Part::getLocation);

    shared_ptr<Part> tmpPart;

    shared_ptr<StoreAnomaly> tmpStoreAnomaly;

    int tmpId;


    for (int i = 0;i < confirmCount;i++)
    {
        tmpStoreAnomaly = this->find_info_current(confirmIdLib[i]);
        tmpPart = tmpInfoManager.find_info(partLib,tmpStoreAnomaly->getPartId(), tmpStoreAnomaly->getLocation());
        if (tmpStoreAnomaly != nullptr && tmpStoreAnomaly->getConfirmStatus() == "未确认")//如果找到对应异常，也找到对应的异常时未确认
        {
            if (tmpPart == nullptr)
            {
                tmpStoreAnomaly->setConfirmStatus("未找到");
            }
            else
            {
                tmpPart->setStockQuantity(tmpStoreAnomaly->getActualQuantity());
                tmpStoreAnomaly->setConfirmStatus("已确认");
            }
        }
    }

    for (auto it = this->allAnomalyLib.end() - this->currentAnomalyLib.size(), it2 = this->currentAnomalyLib.begin(); it != this->allAnomalyLib.end() && it2 != this->currentAnomalyLib.end(); ++it, ++it2)
    {
        tmpId = (*it)->getAnomalyId();
        this->modify_info(tmpId, **it2);
        (*it)->setAnomalyId(tmpId);
    }

    return 0;
}


/*===========================<日志管理类实现>===========================*/
int LogManager::load_logFormFile()
{
    LogEntry tmpLogEntry;
    string line;

    this->logLib.clear();

    ifstream ifs(LogFile, ios::in);
    if (!ifs.is_open())
    {
        ofstream ofs(LogFile, ios::out);
        ofs.close();
        return -1;
    }

    while (getline(ifs, line))
    {
        tmpLogEntry.deSerialize(line);
        this->logLib.push_back(std::make_shared<LogEntry>(tmpLogEntry));
    }
    ifs.close();
    return 0;
}
int LogManager::save_logToFile()
{
    ofstream ofs(LogFile, ios::out);
    if (!ofs.is_open())
    {
        perror("Save open logfile failed!");
        return -1;
    }
    for (auto it = this->logLib.begin();it != this->logLib.end();++it)
        ofs << (*it)->serialize();
    ofs.close();
    return 0;
}

int LogManager::get_log_endId(void)
{
    if (this->logLib.empty())
        return 0;
    else
        return this->logLib.back()->getLogId();
}

int LogManager::out_log(std::string operatorName, Role role, std::string operationDescribe)
{
    LogEntry tmpLogEntry;

    std::time_t currentTime = std::time(nullptr);
    struct tm* localTime = std::localtime(&currentTime);

    std::ostringstream oss;
    // 自定义格式，例如 "YYYY-MM-DD HH:MM:SS"
    oss << std::put_time(localTime, "%Y-%m-%d %H:%M:%S");
    std::string timeString = oss.str();

    operationDescribe.erase(std::remove(operationDescribe.begin(), operationDescribe.end(), '\n'), operationDescribe.end());//除去多余的回车

    tmpLogEntry = LogEntry(LogManager::get_log_endId() + 1, timeString, operatorName, role, operationDescribe);

    this->logLib.push_back(std::make_shared<LogEntry>(tmpLogEntry));
    LogManager::save_logToFile();
    return 0;
}


int LogManager::show_log()
{
    for (auto it = this->logLib.begin();it != this->logLib.end();++it)
        cout << (*it)->serialize();

    return 0;
}
int LogManager::find_log(int logId)
{
    for (auto it = this->logLib.begin();it != this->logLib.end();++it)
    {
        if ((*it)->getLogId() == logId)
            cout << (*it)->serialize();
    }
    return 0;
}
int LogManager::find_log(std::string operatorName)
{
    int count = 0;
    for (auto it = this->logLib.begin();it != this->logLib.end();++it)
    {
       if ((*it)->getOperatorName() == operatorName)
        {
            cout << (*it)->serialize();
            ++count;
        }
    }
    return count;
}
int LogManager::find_log(Role role)
{
    int count = 0;
    for (auto it = this->logLib.begin();it != this->logLib.end();++it)
    {
        if ((*it)->getOperatorRole() == role)
        {
            cout << (*it)->serialize();
            ++count;
        }
    }
        return count;
}
int LogManager::find_log(myTime_t operationTimeStart, myTime_t operationTimeEnd)
{
    std::tm startTm = stringToTm(operationTimeStart);
    std::tm endTm = stringToTm(operationTimeEnd);

    // 检查时间范围是否有效
    if (std::mktime(&startTm) > std::mktime(&endTm))
    {
        std::cerr << "Invalid time range: start time is after end time." << std::endl;
        return -1;
    }

        int count = 0;
        for (const auto& entry : logLib)
        {
            std::tm logTm = stringToTm(entry->getOperationTime());
            if (std::mktime(&logTm) >= std::mktime(&startTm) && std::mktime(&logTm) <= std::mktime(&endTm))
            {
                std::cout << entry->serialize();
                ++count;
            }
        }
        return count;
}

#endif
